home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Mathematics / TeX / TeXit / Utilities / wrap < prev   
Encoding:
Text File  |  1991-08-17  |  1.2 KB  |  46 lines

  1. #!/bin/csh -f
  2. # wrap -- word WRAP  a. el-khadra & p. griffin  7/25/91
  3. #
  4. # usage:  wrap  [ -w nn ] oldfile , nn=width (default=80)
  5. # Notes -- 1.    for wrapped lines , spaces are contracted to one, or two if
  6. #         following ~[.!?]
  7. #       2.    old file replace by wrapped file 
  8. #          
  9. set width = 80 
  10. if ($#argv > 0) then
  11.     if ( "$argv[1]" == '-w') then
  12.         shift
  13.         set a = $argv[1]
  14.         if ($a !~ [0-9]* || $a == 0 || $a >999) then
  15.             echo ww\: Bad width argument $a
  16.             exit 1
  17.         else
  18.             set width = $a 
  19.             shift
  20.         endif
  21.     endif 
  22. if ($#argv > 1) then
  23.     echo usage': wrap[ -w nn] oldfile'   , where nn=width '(default=80)'
  24.     exit 1
  25. endif    
  26. if (! -opr $argv[1] ) then 
  27.     echo  ww\: No file $argv[1]
  28.     exit
  29. endif            
  30. set tf = /tmp/ww:t.$$        
  31. awk "{lt= $width"' \
  32.     if ( length($0) > lt) { \
  33.     line = "" ; ll= 0 \
  34.     for (i=1; i<= NF; i++) { \
  35.         if ( $i ~ /[.\!?]$/ ) { {newpart= $i "  "} ;lp= length($i)+2} \
  36.         else {{newpart=$i " "} ; lp= length($i) +1 } \
  37.         if ( ll + length($i) > lt) { \
  38.             if ( ll =0 ) {print $i} \
  39.             else  {    {print line } ; {line= newpart} ; ll=lp }} \
  40.         else {{newline = line newpart} ; ll += lp ; {line= newline}}} \
  41.     if (ll > 0) {print line} } \
  42. else {print} }' $argv[1] > $tf
  43. mv $tf $argv[1]
  44.  
  45.